package dao;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import dto.UserDto;

public class UserDaoImpl implements UserDao {
	private JdbcTemplate template;

	public void setDataSource(DataSource dataSource) {
		this.template = new JdbcTemplate(dataSource);
	}

	private static final String INSERT_USER_INSERT = "INSERT INTO member(id, passwd, name, email, phone, favorite) "
			+ " VALUES(?,?,?,?,?,?)";

	@Override
	public int insertUser(UserDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(INSERT_USER_INSERT,
				new Object[] { bean.getId(),bean.getPasswd(), bean.getName(), bean.getEmail(),
						bean.getPhone(), bean.getFavorite() });
	}

	private static final String SELECT_USER_LOGIN_PASS = "SELECT * FROM member WHERE id = ? AND passwd = ?";
	@Override
	public UserDto loginUser_passwd(String id, String passwd) {
		// TODO Auto-generated method stub
		RowMapper<UserDto> mapper = new BeanPropertyRowMapper<UserDto>(
				UserDto.class);
		return this.template.queryForObject(SELECT_USER_LOGIN_PASS, mapper, id, passwd);
	}
	
	private static final String DELECT_USER_REMOVE = "DELETE FROM member WHERE id = ? AND passwd = ?";

	@Override
	public int removeUser(UserDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(DELECT_USER_REMOVE, bean.getId(), bean.getPasswd());
	}

	private static final String UPDATE_USER_UPDATE = "UPDATE member SET passwd=?, name = ?, email=?, phone=?, favorite=? WHERE id=?";

	@Override
	public int updateUser(UserDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(
				UPDATE_USER_UPDATE,
				new Object[] { bean.getPasswd(), bean.getName(),
						bean.getEmail(), bean.getPhone(), bean.getFavorite(),
						bean.getId() });
	}
}
